<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Scripts\Cronds;

use Srv\Libs\Boot;
use Srv\Libs\Common\CommTime;
use Srv\Libs\Daemon\PhpDaemon;
use Srv\Libs\Storage\DataMysql;
use Srv\Libs\Storage\DataRedis;

require_once dirname(__DIR__, 2).'/Libs/Boot.php';
Boot::runConf('');
set_time_limit(0);

// 定时循环执行
(new class extends PhpDaemon
{
    protected string $defaultPidPath                = '';           // default pid file path
    protected string $defaultLogPath                = '';           // default log file path
    protected string $defaultConfPath               = '';           // default config file path
    protected int $defaultSleep                     = 5;            // default Sleep time
    private ?DataMysql $DbAuto                      = null;         // DB-Write
    private ?DataRedis $CcAuto                      = null;         // CcAuto

    /**
     *  constructor.
     */
    public function __construct()
    {
        $classFileName              = trim(preg_replace("'\\\\'", '_', __CLASS__), '_');
        (preg_match("/^\S+@anonymous\S+/i", $classFileName)) && $classFileName = basename(__FILE__);
        $pathLogs                   = Boot::getLogsPath();
        $this->defaultPidPath       = $pathLogs.'Cronds/'.$classFileName.'.pid';
        $this->defaultLogPath       = $pathLogs.'Cronds/'.$classFileName.'.log';
        $this->pidUrl               = $this->defaultPidPath;
        $this->logUrl               = $this->defaultLogPath;
        $this->confUrl              = $this->defaultConfPath;
        $this->sleep                = $this->defaultSleep;
        parent::__construct();
    }

    /**
     * runInit
     */
    public function runInit():void
    {
        $pId                        = getmypid();
        $this->DbAuto               = DataMysql::tag('auto', 'DBW_'.$pId);
        $this->CcAuto               = DataRedis::tag('auto', 'CC_'.$pId);
    }

    /**
     * run
     */
    public function run():void
    {
        echo CommTime::getString('Y-m-d H:i:s')."\n";
    }
});